package org.eclipse.epsilon.emc.lom.dt;

import org.eclipse.core.resources.IFile;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.epsilon.common.dt.launching.dialogs.AbstractModelConfigurationDialog;
import org.eclipse.epsilon.common.dt.launching.dialogs.BrowseWorkspaceUtil;
import org.eclipse.epsilon.emc.lom.LiveObjectModel;
import org.eclipse.epsilon.util.emf.BrowseEPackagesListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class LiveObjectModelConfigurationDialog extends AbstractModelConfigurationDialog {

	
	protected String getModelName() {
		return "Live Object Model";
	}

	
	protected String getModelType() {
		return "LiveObjectModel";
	}
	
	protected Label fileTextLabel;
	protected Text fileText;
	protected Button browseModelFile;
	protected Button filebasedButton;
	protected Text metaModelUriText;
	protected Button browseMetamodelUri;
	protected Label modelFileLabel;
	protected Label metaModelUriLabel;
	
	protected void createGroups(Composite control) {
		createNameAliasGroup(control);
		createFilesGroup(control);
	}
	
	protected Composite createFilesGroup(Composite parent) {
		final Composite groupContent = createGroupContainer(parent, "Files/URIs", 3);
				
		fileTextLabel = new Label(groupContent, SWT.NONE);
		fileTextLabel.setText("File: ");
		
		fileText = new Text(groupContent, SWT.BORDER);
		fileText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		browseModelFile = new Button(groupContent, SWT.NONE);
		browseModelFile.setText("Browse Workspace...");
		browseModelFile.addListener(SWT.Selection, new BrowseWorkspaceForModelsListener2(fileText, "Live Object Models in the workspace", "Select a Live Object Model"));

		metaModelUriLabel = new Label(groupContent, SWT.NONE);
		metaModelUriLabel.setText("Meta-model URI: ");
		metaModelUriText = new Text(groupContent, SWT.BORDER);
		metaModelUriText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		browseMetamodelUri = new Button(groupContent, SWT.NONE);
		browseMetamodelUri.setText("Browse EPackages...");
		browseMetamodelUri.addListener(SWT.Selection, new BrowseEPackagesListener() {

			@Override
			public void selectionChanged(EPackage ePackage) {
				metaModelUriText.setText(ePackage.getNsURI());
			}
			
		});
		
		GridData uriTextGridData = new GridData(GridData.FILL_HORIZONTAL);
		uriTextGridData.horizontalSpan = 2;

		groupContent.layout();
		groupContent.pack();
		return groupContent;
	}
	
	protected class BrowseWorkspaceForModelsListener2 implements Listener{
		
		private Text text = null;
		private String title = "";
		private String prompt = "";
		
		public BrowseWorkspaceForModelsListener2(Text text, String title, String prompt){
			this.text = text;
		}
		
		public void handleEvent(Event event) {
			String file = browseFilePath(getShell(), 
					title, prompt, "", null);
			if (file != null){
				text.setText(file);
			}
		}
		
		public String browseFilePath(Shell shell, String title, String message, String filter, Image image){
			
			IFile file = BrowseWorkspaceUtil.browseFile(shell, title, message, filter, image);
			if (file != null) {
				return file.getLocation().toOSString();
			}
			else {
				return null;
			}
		}
	}
	
	protected void loadProperties(){
		super.loadProperties();
		if (properties == null) return;
		fileText.setText(properties.getProperty(LiveObjectModel.PROPERTY_DBNAMEPATH));
		metaModelUriText.setText(properties.getProperty(LiveObjectModel.PROPERTY_METAMODEL_URI));
		
	}
	
	protected void storeProperties(){
		super.storeProperties();
		properties.put(LiveObjectModel.PROPERTY_DBNAMEPATH, fileText.getText());
		properties.put(LiveObjectModel.PROPERTY_METAMODEL_URI, metaModelUriText.getText());
	}
}
